var e = require;
var t = module;
var o = ["_onTouchStart", "_onTouchMove", "_onTouchEnd", "_onTouchCancel"],
    n = {
        _prefix: "_",
        _plugins: [],
        registerPlugin: function (e) {
            var t = this;
            Array.isArray(e) || (e = [e]);
            e.forEach(function (e) {
                if (
                    !t._plugins.find(function (t) {
                        return t.name === e.name || t === e;
                    })
                ) {
                    t._plugins.push(e);
                    e.onRegister && e.onRegister();
                }
            });
        },
        unregisterPlugin: function (e) {
            if (e || e.name) {
                var t = this._plugins.findIndex(function (t) {
                    return t.name === e.name || t === e;
                });
                t >= 0 && this._plugins.splice(t, 1);
            }
        },
        _getComponentName: function (e) {
            return e.name.match(/<.*>$/)[0].slice(1, -1);
        },
        bindComponent: function (e, t) {
            var n = this;
            e.$options = t || {};
            var i = e.node;
            i._components.forEach(function (e) {
                var t = n._getComponentName(e);
                i[(t = "$" + t)] = e;
            });
            this._bindTouchEvent(i, e, o);
            this._bindNode(e.node, e);
        },
        bindNode: function (e, t, o) {
            var n = this;
            t.$options = o || {};
            if (t.$collector) {
                if (t.$collector.node === e) return;
                delete t.$collector.node;
                Object.keys(t.$collector).forEach(function (e) {
                    delete t[e];
                });
            }
            t.$collector = {node: e};
            e._components.forEach(function (e) {
                var o = n._getComponentName(e);
                t[(o = "$" + o)] = e;
                t.$collector[o] = e;
            });
            this._bindStartByPlugins(e, t);
            this._bindTouchEvent(e, t);
            this._bindNode(e, t);
            this._bindEndByPlugins(e, t);
        },
        _bindStartByPlugins: function (e, t) {
            this._plugins.forEach(function (o) {
                o.onBindStart && o.onBindStart(e, t);
            });
        },
        _bindEndByPlugins: function (e, t) {
            this._plugins.forEach(function (o) {
                o.onBindEnd && o.onBindEnd(e, t);
            });
        },
        _bindNode: function (e, t) {
            var o = this,
                i = e,
                r = !1;
            i.name[0] === this._prefix &&
                i._components.forEach(function (e) {
                    var a = o._getComponentName(e);
                    if (i[(a = "$" + a)] && t.$options.debug) cc.warn(a + " property is already exists");
                    else {
                        i[a] = e;
                        n.isFunction(e.onBind) && e.onBind(t);
                        if (e instanceof Thor && !e.isPermeateUIKiller) {
                            r || e === t || (r = !0);
                            !i.active && r && e.bindHammer();
                        }
                    }
                });
            this._checkNodeByPlugins(i, t) &&
                !r &&
                i.children.forEach(function (e) {
                    var n = e.name;
                    if (n[0] === o._prefix) {
                        var r = n.indexOf("$");
                        if (-1 !== r) {
                            e.$eventName = n.substr(0, r);
                            e.$ = n.substr(r + 1);
                            n = e.$eventName + e.$[0].toUpperCase() + e.$.substr(1);
                            e.name = n;
                        }
                        if (t[n] && t.$options.debug) {
                            cc.warn(t.name + "." + n + " property is already exists");
                            return;
                        }
                        o._bindTouchEvent(e, t);
                        t[n] = e;
                        t.$collector && (t.$collector[n] = e);
                    } else i[n] || (i[n] = e);
                    o._bindNode(e, t);
                });
        },
        _getTouchEventName: function (e, t) {
            var o = e.$eventName || e.name;
            if (!(o && o.length > 0 && "_" == o[0])) return t ? "_on" + o + t : [];
            o = o[this._prefix.length].toUpperCase() + o.slice(this._prefix.length + 1);
            return t
                ? "_on" + o + t
                : [
                      "_on" + o + "TouchStart",
                      "_on" + o + "TouchMove",
                      "_on" + o + "TouchEnd",
                      "_on" + o + "TouchCancel"
                  ];
        },
        _bindTouchEvent: function (e, t, o) {
            var n = this;
            if (!e.getComponent(cc.EditBox)) {
                var i = o || this._getTouchEventName(e),
                    r = [
                        cc.Node.EventType.TOUCH_START,
                        cc.Node.EventType.TOUCH_MOVE,
                        cc.Node.EventType.TOUCH_END,
                        cc.Node.EventType.TOUCH_CANCEL
                    ];
                i.forEach(function (o, i) {
                    (t[o] || e.getComponent(cc.Button) || e.getComponent(cc.Toggle)) &&
                        e.on(r[i], function (i) {
                            var r = i.currentTarget;
                            if (!1 !== r.interactable && !1 !== r.active) {
                                var a = r.getComponent(cc.Button) || r.getComponent(cc.Toggle);
                                if (a && !1 === a.interactable) n._doTouchDisableEvent(e, t, i);
                                else {
                                    var c = t[o],
                                        u = c || (a && a.clickEvents.length);
                                    u && n._beforeHandleEventByPlugins(r, i, !!c);
                                    var s,
                                        p = !1;
                                    if (i.type === cc.Node.EventType.TOUCH_END) {
                                        var d = t._onTouchEndFilter;
                                        d && (p = !!d.call(t, r, i));
                                    }
                                    if (!p && c)
                                        if (!1 === (s = c.call(t, r, i))) r._touchListener.setSwallowTouches(!1);
                                        else {
                                            e._touchListener.setSwallowTouches(!0);
                                            i.stopPropagation();
                                        }
                                    !p && u && n._afterHandleEventByPlugins(r, i, !!c, s);
                                }
                            }
                        });
                });
                this._bindTouchLongEvent(e, t);
            }
        },
        _bindTouchLongEvent: function (e, t) {
            var o = e,
                i = this._getTouchEventName(o, "TouchLong"),
                r = t[i];
            if (n.isFunction(r)) {
                o._touchLongTimer = null;
                o.on(cc.Node.EventType.TOUCH_END, function () {
                    if (o._touchLongTimer) {
                        clearTimeout(o._touchLongTimer);
                        o._touchLongTimer = 0;
                        delete o.interactable;
                    }
                });
                o.on(cc.Node.EventType.TOUCH_START, function (e) {
                    o._touchLongTimer = setTimeout(function () {
                        o.interactable = !!r.call(t, o, e);
                        o._touchLongTimer = 0;
                    }, o.touchLongTime || 1e3);
                });
            }
        },
        _doTouchDisableEvent: function (e, t, o) {
            var i = t[this._getTouchEventName(e, "TouchDisable")],
                r = null;
            if (n.isFunction(i)) {
                i && (r = i.call(t, e, o));
                this._afterHandleEventByPlugins(e, o, !!i, r, !1);
            } else this._afterHandleEventByPlugins(e, o, !!i, r, !1);
        },
        _checkNodeByPlugins: function (e, t) {
            return !this._plugins.find(function (o) {
                return o.onCheckNode ? !1 === o.onCheckNode(e, t) : null;
            });
        },
        _beforeHandleEventByPlugins: function (e, t, o) {
            this._plugins.forEach(function (n) {
                n.onBeforeHandleEvent && n.onBeforeHandleEvent(e, t, o);
            });
        },
        _afterHandleEventByPlugins: function (e, t, o, n, i) {
            void 0 === i && (i = !0);
            this._plugins.forEach(function (r) {
                r.onAfterHandleEvent && r.onAfterHandleEvent(e, t, o, n, i);
            });
        },
        isFunction: function (e) {
            return "function" == typeof e;
        }
    };
window.uikiller = t.exports = n;
